/*
===========================================================================
Copyright (C) 2013-2014 Avotu Briezhaudzetava
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program. If not, see http://www.gnu.org/licenses/.

===========================================================================
*/

//
// user.cpp (main.cpp is too crowded)
//

//
// includes
//
#include "main.h"
#include <QSqlQuery>
#include <QSqlError>
#include "hash.h"

/*
================
addUser
================
*/
void Main::addUser( const QString &name, const QString &password, UserEntry::Flags flags ) {
    QSqlQuery query;

    // avoid duplicates
    if ( this->userForName( name ) != NULL )
        return;

    // perform database update and select last row    
    if ( !query.exec( QString( "insert into users values ( null, '%1', '%2', '%3' )" )
                      .arg( name )                      // 1
                      .arg( Hash::encrypt( password ))  // 2
                      .arg( flags )                     // 3
                      )) {
        this->error( StrSoftError + QString( "could not add user, reason: \"%1\"\n" ).arg( query.lastError().text()));
    }
    query.exec( QString( "select * from users where id=%1" ).arg( query.lastInsertId().toInt()));

    // get last entry and construct internal entry
    while ( query.next())
        this->userList << new UserEntry( query.record(), "users" );
}

/*
================
removeUser
================
*/
void Main::removeUser( const QString &name ) {
    UserEntry *uPtr = NULL;
    QSqlQuery query;

    // find precursor
    uPtr = this->userForName( name );

    // failsafe
    if ( uPtr == NULL )
        return;

    // remove user from db
    // TODO: or mark as inactive in permissions???
    // we still need names of delted users
    query.exec( QString( "delete from users where id=%1" ).arg( uPtr->id()));

    // remove from display
    this->userList.removeAll( uPtr );
}

/*
================
loadUsers
================
*/
void Main::loadUsers() {
    QSqlQuery query;

    // read stuff
    query.exec( "select * from users" );

    // store entries
    while ( query.next())
        this->userList << new UserEntry( query.record(), "users" );

    // check for admin
    if ( this->userList.isEmpty())
        this->addUser( Common::superUserName, Common::superUserPassword, UserEntry::Superuser );
}

/*
================
userForId
================
*/
UserEntry *Main::userForId( int id ) const {
    foreach ( UserEntry *uPtr, this->userList ) {
        if ( uPtr->id() == id )
            return uPtr;
    }
    return NULL;
}

/*
================
userForName
================
*/
UserEntry *Main::userForName( const QString &name ) const {
    foreach ( UserEntry *uPtr, this->userList ) {
        if ( !QString::compare( name, uPtr->name()))
            return uPtr;
    }
    return NULL;
}
